package com.cslg.ac.demo.service.search;

import com.cslg.ac.demo.constant.LikeType;
import com.cslg.ac.demo.constant.PostKind;
import com.cslg.ac.demo.constant.PostType;
import com.cslg.ac.demo.dao.host.HostDAO;
import com.cslg.ac.demo.dao.like.LikeDAO;
import com.cslg.ac.demo.dao.pet.PetAdoptDAO;
import com.cslg.ac.demo.dao.pet.PetCommonDAO;
import com.cslg.ac.demo.dao.pet.PetFosterDAO;
import com.cslg.ac.demo.dao.pet.UserPetDAO;
import com.cslg.ac.demo.dao.post.PostDAO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.entity.post.Post;
import com.cslg.ac.demo.exception.ParamIllegalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@PropertySource(value = {"classpath:search.properties"},encoding="utf-8")
public class SearchService {

    @Autowired
    PetAdoptDAO petAdoptDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    PetCommonDAO petCommonDAO;
    @Autowired
    HostDAO hostDAO;
    @Autowired
    PostDAO postDAO;
    @Autowired
    LikeDAO likeDAO;
    @Autowired
    UserPetDAO userPetDAO;
    @Value("${teamType}")
    int teamType;
    @Value("${personType}")
    int personType;
    @Value("${petType}")
    int petType;
    @Value("${hostType}")
    int hostType;




    public List<Integer> petsByThree( int offset, int num, String address, Integer gender, String variety){
        if(gender!= -1 && !"null".equals(address) &&  !"null".equals(variety)){
            return userPetDAO.selectByAddressAndGenderAndType(address, 1, gender, variety, offset, num);
        }
        if(gender != -1 && !"null".equals(address)){
            return userPetDAO.selectByAddressAndGender(address, 1, gender, offset, num);
        }
        if(gender != -1 &&  !"null".equals(variety)){
            return petCommonDAO.selectByGenderAndType(gender, variety, offset, num);
        }
        if( !"null".equals(address) && !"null".equals(variety)){

            return userPetDAO.selectByAddressAndType(address, 1, variety, offset, num);
        }
        if(gender != -1){
            return petCommonDAO.selectByGender(gender, offset, num);
        }
        if(!"null".equals(address)){
            return userPetDAO.selectByAddress(address, 1, offset, num);

        }
        if( !"null".equals(variety)){
            return petCommonDAO.selectByType(variety, offset, num);

        }

        return null;
    }


    public Integer[] postByType(int postType, int type, int offset, int num, int chooseType, int userId){
        switch (postType){
            case PostKind.COMMENT:
                return commentByType(type, offset, num);
            case PostKind.COMMUNITY_POST:
            case PostKind.PERSONAL_POST:
                return communityAndPersonPostByType(type, offset, num, chooseType, userId, postType);
            case PostKind.EVALUATION:
                return evaluationByType(type, offset, num);
            default:throw new ParamIllegalException("无此postType！");
        }

    }


    public Integer[] evaluationByType(int type, int offset, int num){
        return postDAO.selectPostByType(type, offset, num);

    }

    public Integer[] commentByType(int type, int offset, int num){
        return postDAO.selectCommentByType(type, offset, num);
    }

    public Integer[] communityAndPersonPostByType(int type, int offset, int num,
                                                  int chooseType, int userId, int postType){
        Integer[] ids = null;
        List<Integer[]> allIds = new ArrayList<>();
        List<Integer> postsId = new ArrayList<>();
        if(chooseType == 0){
            ids = postDAO.selectPostByType(type, offset, num);
        }else {
            int[] like = likeDAO.select(userId, LikeType.USER);
            if(like.length > 0){
                for(int i = 0;i < like.length;i++){
                    Integer[] test = (postDAO.selectUserPostByUser(userId, postType, 0, 100));
                    allIds.add(postDAO.selectUserPostByUser(like[i], postType, 0, 100));
                }
            }
            for(int i = 0; i < allIds.size();i++){
                for (Integer get : allIds.get(i)) {
                    Integer getId = postDAO.selectPostByIdAndType(get, type);
                    if(getId != null){
                        postsId.add(postDAO.selectPostByIdAndType(get, type));
                    }

                }
            }
            Collections.sort(postsId);
            if(postsId.size() > 0){
                if(offset + num  > postsId.size()){
                    postsId = postsId.subList(offset, postsId.size());
                }else {
                    postsId = postsId.subList(offset, offset + num );
                }
                ids = new Integer[postsId.size()];
                for(int i = 0; i < postsId.size();i++){
                    ids[i] = postsId.get(i);
                }
            }
        }
        return ids;
    }

    public Integer[] usersByAdress(String address, int userType, int offset, int num){
        if(userType == teamType){
            return teamInfoDAO.selectIdByAddress(address,offset,num);
        }else if(userType == personType){
            return personInfoDAO.selectByCity(address,offset,num);
        }else if(userType == hostType){
            return hostDAO.selectIdByAddress(address,offset,num);
        }
        return null;
    }



    public Integer userByPhone(String phone){
        Integer id = userDAO.selectIdByPhone(phone);
        return id;
    }


    public Integer[] usersByName(String name, int userType, int offset, int num) {
        if(userType == teamType ){
            return teamInfoDAO.selectIdByName(name, offset, num);
        }else if(userType == personType){
            return personInfoDAO.selectIdByName(name, offset, num);
        }else if(userType == hostType){
            return hostDAO.selectIdByName(name,offset,num);
        }
        return null;
    }

    public Integer userbyEmail(String email){
        Integer id = personInfoDAO.selectIdByMail(email);
        if(id != null){
            return id;
        }else {
            id = teamInfoDAO.selectIdByEmail(email);
        }
        return id;

    }
}
